<template>
    <div class="goodsinfo">

        <transition 
        @before-enter="beforeEnter"
        @enter="enter"
        @after-enter="afterEnter">
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>

        <!-- 商品轮播图区域 -->
        <div class="mui-card">
        <div class="mui-card-content">
			<div class="mui-card-content-inner">
                <swiper :list="lunbotu"></swiper>
			</div>
		</div>
        </div>

        <!-- 商品购买区域 -->
        <div class="mui-card">
			<div class="mui-card-header">{{goodsinfo.title}}</div>
			<div class="mui-card-content">
				<div class="mui-card-content-inner">
                    <p class="price">
                        市场价：<del>￥{{goodsinfo.market_price}}</del>&nbsp;&nbsp;销售价：<span class="nowprice">￥{{goodsinfo.sell_price}}</span>
                    </p>
                    <p>购买数量：<numbox @changeCount="getCount" :max="goodsinfo.stock_quantity"></numbox></p>
                    <p>
                        <button type="button" class="mui-btn mui-btn-primary" >立即购买</button>
                        <button type="button" class="mui-btn mui-btn-danger" @click="addToshopcar">加入购物车</button>
                    </p>
				</div>
			</div>
		</div>

        <!-- 商品参数区域 -->
        <div class="mui-card">
				<div class="mui-card-header">商品参数</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p>商品货号：{{goodsinfo.goods_no}}</p>
                        <p>库存情况：{{goodsinfo.stock_quantity}}件</p>
                        <p>上架时间：{{goodsinfo.add_time | dataFormat}}</p>
					</div>                                 
				</div>
				<div class="mui-card-footer">
                    <button type="button" class="mui-btn mui-btn-primary mui-btn-outlined" @click="goDesc(id)">图文介绍</button>
                    <button type="button" class="mui-btn mui-btn-danger mui-btn-outlined" @click="goComment(id)">商品评论</button>
                </div>
			</div>
    </div>    
</template>


<script>
import swiper from "../subcomponents/swiper.vue";
import numbox from "../subcomponents/goodsinfo_numbox.vue";
export default {
    data() {
        return {
            id: this.$route.params.id,
            lunbotu: [],
            goodsinfo: {},
            ballFlag: false, // 控制小球的显示和隐藏的标识符
            selectedCount: 1,
        }
    },
    created() {
        this.getlunbotu();
        this.getgoodsinfo();
    },
    methods: {
        getlunbotu() {
            this.$http.get('api/getthumimages/'+ this.id).then(result => {
                if(result.body.status === 0) {
                    // this.lunbotu = result.body.message;
                    result.body.message.forEach(element => {
                        element.img = element.src;
                    });
                    this.lunbotu = result.body.message;
                    console.log(this.lunbotu);
                }
            })
        },
        getgoodsinfo() {
            this.$http.get('api/goods/getinfo/'+ this.id).then(result => {
                if(result.body.status === 0) {

                    this.goodsinfo = result.body.message[0];
                    console.log(this.goodsinfo);
                }
            })
        },
        goDesc(id) {
            this.$router.push({name:'goodsdesc',params: {id}});
        },
        goComment(id) {
            this.$router.push({name:'goodscmt',params: {id}});
        },
        addToshopcar() {
            this.ballFlag = !this.ballFlag;
          //  { id: 商品的id, count: 要买的数量, price: 商品的单价, selected: false(是否选中)}
            var goods = { id: this.id, count: this.selectedCount, price: this.goodsinfo.sell_price, selected: true};
            this.$store. commit("addToCar",goods);
        },
        beforeEnter(el) {
            el.style.transform = "translate(0,0)";
        },
        enter(el, done) {
            el.offsetWidth;

            // 小球动画优化思路
            // 1、分析动画不准确的本质原因  我们把小球 最终位移到的位置是确定的  已经局限在了某一分辨率下的 滚动条未滚动的情况下
            // 2、只要分辨率和测试的时候不一样  或者  滚动条有一定的滚动距离之后，问题就出现了
            // 3、因此  我们经过分析  得到结论： 不能把 位置的距离（横纵坐标） 直接写死了 而是应该  根据不同情况  动态的计算这个坐标值
            // 4、分析得出解决方案： 先得到 徽标的坐标 在得到 小球的坐标  然后求差就可以了
            // 获取方法： domObject.getBoundingClientRect()
           
            // 获取小球在页面的位置  (ref获取组件内部的元素)
            const ballPosition = this.$refs.ball.getBoundingClientRect();
            // 获取徽标在页面中的位置
            const badgePosition = document.getElementById("badge").getBoundingClientRect();
            
            
            const xDist = badgePosition.left - ballPosition.left;
            const yDist = badgePosition.top - ballPosition.top;
           
            el.style.transform = `translate(${xDist}px,${yDist}px)`;
            el.style.transition = "all 0.6s cubic-bezier(.4,-0.3,1,.68)";
            done();
        },
        afterEnter(el) {
            this.ballFlag = !this.ballFlag;
        },
        getCount(count) {
             this.selectedCount = count;
             console.log("父组件拿到的值 "+this.selectedCount);
        },

    },
    components: {
        swiper: swiper,
        numbox,
    }
}
</script>

<style lang="scss" scoped>
.goodsinfo {
    background-color: #eee;
    overflow: hidden;
   
    .nowprice {
        color: red;
        font-size: 14px;
        font-weight: bold;
    }
    .mui-card-footer {
        display: block;
        button {
            margin: 10px 0;
        }
    }
    .ball {
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background-color: red;
        position: absolute;
        z-index: 99;
        top: 413px;
        left: 147px;
    }
    // v-enter,
    // v-leave-to {
    //     opacity: 0;
    // }
    // v-enter-active,
    // v-leave-active {
    //     transform: all 1s ease;
    // }
}

</style>

